package com.deepfos.outreach.component;

import com.deepfos.outreach.domain.CashFlowPushDto;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.entity.data.Slot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 初始化参数检查组件
 */
@Component("checkCmp")
public class CheckCmp extends NodeComponent {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void process() throws Exception {
        //拿到请求参数
        List<String> cf_ids = this.getSlot().getRequestData();

        Slot slot = this.getSlot();
        HashMap<String, Object> result = new HashMap<>();
        slot.setResponseData(result);
        result.put("errors", new HashMap<String, Object>());

        if (Objects.isNull(cf_ids)) {
            throw new IllegalArgumentException("cf_id为空！");
        }

        for (String cf_id : cf_ids) {
            CashFlowPushDto query = (CashFlowPushDto) jdbcTemplate.queryForObject("Select cf_id, contract_no, contract_name, lease_type, user_id, lease_start_date, lease_end_date, cf_amount, company_code, supplier_code, cf_date, account_lease_exempt, sys_object_id, sys_contract_id, cf_status_old, cf_vat_invoice_type, period from cash_flow_push where cf_id = ?", new BeanPropertyRowMapper(CashFlowPushDto.class), cf_id);
            if (Objects.isNull(query)) {
                if (Objects.nonNull(result.get("errors"))) {
                    Map errors = (HashMap) result.get("errors");
                    errors.put(cf_id, "未找到对应数据！");
                }
            }
        }

        /***这里Mock参数验证过程***/
        log.info("参数验证完成");
    }
}
